package com.example.demo.controller;

import com.example.demo.entify.AboveProblems;
import com.example.demo.result.Result;
import com.example.demo.result.ResultFactory;
import com.example.demo.service.aboveProblemsService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@Component //使spring管理
@EnableScheduling //定时任务注解
@RestController
@CrossOrigin
@RequestMapping("/older")
public class aboveProblemsController {
	@Autowired
	private aboveProblemsService aboveProblemsService;

	@PostMapping("/savequestion")
	private Result save(@RequestBody AboveProblems aboveProblems) {
		int i = aboveProblemsService.insert(aboveProblems);
		return i > 0 ? ResultFactory.buildSuccessFactory(true) : ResultFactory.buildFailFactory("添加失败");
	}

	@PostMapping("/checkAllproblems")
	public Result checkAllproblems(String userName) {
		List<AboveProblems> problems = aboveProblemsService.Problems(userName);
		return ResultFactory.buildSuccessFactory(problems);

	}

	@PostMapping("/deleteProblems")
	public Result deleteProblems(Integer id) {
		int i = aboveProblemsService.deleteProblems(id);
		return i > 0 ? ResultFactory.buildSuccessFactory(true) : ResultFactory.buildFailFactory("删除成功");
	}

	@GetMapping("/acceptedProblem")
	public Result AllAcceptedProblem() {
		List<AboveProblems> problems = aboveProblemsService.AcceptedProblem();
		return ResultFactory.buildSuccessFactory(problems);

	}

	@PostMapping("/updateProblemState")

	public Result updateProblemState(Integer id, Integer orderId) {

		int i = aboveProblemsService.updatePromState(id, orderId);
		return i > 0 ? ResultFactory.buildSuccessFactory(true) : ResultFactory.buildFailFactory("修改失败");
	}
	@PostMapping("/updateProblemState1")
	
	public Result updateProblemState1(Integer id) {
		int i = aboveProblemsService.updatePromState1(id);
		return i > 0 ? ResultFactory.buildSuccessFactory(true) : ResultFactory.buildFailFactory("修改失败");
	}

	@PostMapping("/updateProblemState2")
	public Result updateProblemState2(Integer id) {
		int i = aboveProblemsService.updatePromState2(id);
		return i > 0 ? ResultFactory.buildSuccessFactory(true) : ResultFactory.buildFailFactory("修改失败");
	}
	
	@GetMapping("/checkProblem")
	public Result checkProblem(String userName) {
		List<AboveProblems> myProblems = aboveProblemsService.checkProblem(userName);
		return ResultFactory.buildSuccessFactory(myProblems);
	}
	
	@GetMapping("/checkProblemId")
	public Result checkProblemId(Integer id) {
			int problemstate = aboveProblemsService.aboveProblemid(id);
		return ResultFactory.buildSuccessFactory(problemstate);
	}
	
	 @Scheduled(cron = "0 0 1 1 * ?")
	    public void initTimerOne(){
		 List<AboveProblems> problemstate2list = aboveProblemsService.ProblemBystate();
		 if (problemstate2list!=null) {
			 for (AboveProblems aboveProblems : problemstate2list) {
				 int updateState= aboveProblemsService.updatePromState2(aboveProblems.getId());
				 			 
			}
			 			
		}
	        System.out.println("由于长时间未确认已解决，系统自动确认问题已解决");
	        
	    }
	
	
	

}
